package com.common.controller;

import com.common.entity.admin;
import com.common.service.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
public class adminController {
    @Autowired
    private service adminService;

    @GetMapping("/login")
    public admin Login(@RequestParam("username") String username,@RequestParam("password") String password)
    {
        return adminService.Login(username,password);
    }

    @GetMapping("/checkToken")
    public boolean check(HttpServletRequest req)
    {
        String token = req.getHeader("Authorazition");
        System.out.println(token);
        return adminService.check(token);
    }

    @GetMapping("/get")
    public List<admin> get()
    {
        return adminService.getAll();
    }

    @GetMapping("/add")
    public admin add(@RequestParam("username") String username,@RequestParam("password") String password,
    @RequestParam(name="phone",required = false) Long phone,@RequestParam("identity") String identity)
    {
        admin a = new admin();
        a.setUsername(username);
        a.setPassword(password);
        a.setPhone(phone);
        a.setIdentity(identity);
        return adminService.update(a);
    }
    @GetMapping("/update")
    public admin update(@RequestParam("aid") Integer aid,@RequestParam("username") String username,@RequestParam("password") String password,
                        @RequestParam(name="phone",required = false) Long phone,@RequestParam("identity") String identity)
    {
        admin a = new admin();
        a.setAid(aid);
        a.setUsername(username);
        a.setPassword(password);
        a.setPhone(phone);
        a.setIdentity(identity);
        System.out.println(a);
        return adminService.update(a);
    }
    @GetMapping("/deleteOne")
    public void deleteOne(@RequestParam("0") Integer aid)
    {
        System.out.println(aid);
        adminService.deleteOne(aid);
    }
    @PostMapping("/deleteMany")
    public void deleteOne(@RequestBody List<Integer> ids)
    {
        System.out.println(ids);
        adminService.deleteMany(ids);
    }
}
